package com.test.domain;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import com.opensymphony.xwork2.ActionContext;

/**
 * 用户
 * @author tyg
 * 
 */
@SuppressWarnings("unchecked")
public class User {
	private Integer id;
	private Department department;
	private Set<Role> roles = new HashSet<Role>();

	private String loginName; // 登录名
	private String password; // 密码
	private String name; // 真实姓名
	private String gender; // 性别
	private String phoneNumber; // 电话号码
	private String email; // 电子邮件
	private String description; // 说明

	
	/**
	 * 
	 * @param name 
	 * 在判断用户是否具有左侧菜单栏权限时，<br>
	 * left.jsp页面传入从菜单列表中循环出的权限对象，取得其privName值
	 * @return
	 */
	public boolean hasPriviledgeByName(String privName){
		System.out.println("传入的权限名称为："+privName);
		//超级管理员默认具有所有权限
		if(isAdmin()){
			return true;
		}
		//如果是普通管理员，就需要判断其权限
		for(Role role : roles){
			for(Priviledge p : role.getPriviledges()){
				if(p.getName().equals(privName)){
					return true;
				}
			}
		}
		return false;
	}
	public boolean hasPriviledgeByUrl(String privUrl){
		//例如页面中的action请求为:删除：action="user_delete?id=%{id}"
    	//新增和修改：action="user_editUI?id=%{id}",action="user_addUI"
    	//但在priviledge中保存的url为：/user_delete、/user_add、/user_edit.
		if(isAdmin()){
			return true;
		}
		int index = privUrl.indexOf("?");
		if(index > -1){
			privUrl = privUrl.substring(0, index);
		}
		if(privUrl.endsWith("UI")){
			privUrl = privUrl.substring(0, privUrl.length()-2);
		}
		System.out.println("传入的权限URL为："+privUrl);
		//如果是普通管理员，就需要判断其权限
		Collection<String> allPriviledgeUrls = (Collection<String>)ActionContext.getContext().getApplication().get("allPriviledgeUrls");
		//如果不需要权限控制,比如登录后既可以使用的权限,由于传过来的privUrl是从action：<s:a action= >请求中获取的，
		//因此就会包括不需要权限控制的action,比如:退出系统<s:a action="loginout_logout">
		if(!allPriviledgeUrls.contains(privUrl)){
			return true;
			
		}else{
			for(Role role : roles){
				for(Priviledge p : role.getPriviledges()){
					if(privUrl.equals(p.getUrl())){
						return true;
					}
				}
			}
		}
		return false;
	}
	/**
	 * 判断用户是否是超级管理员，但是这里写死了，超级管理员的名称就为"admin/Admin";
	 * @return
	 */
	public boolean isAdmin(){
		return "admin".equals(loginName);
	}
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}
